<?php
/* terenacapi.php
 */
require 'UserRepository.php';
require 'User.php';
require 'MessageRepository.php';
require 'Message.php';
require 'database.inc.php';
require './API/post.php';
require './API/login.php';


//http://php.fer.hr/~mg44268/terenacapi.php?action=login&username=mgustin&password=fa49d12b5c877f71ad9e7d8f5d02b6d5ae407c5a

if (isset($_REQUEST['action'])) {
    if ($_REQUEST['action'] == 'login') {
        $data = login($db);
    }
    if ($_REQUEST['action'] == 'post') {
       $data = post($db);       
    }
    $handle = fopen("log.txt", "a");    
    fwrite($handle, '\n\r\n\r'.date('r', time()));
    fwrite($handle, '\n\rData:\n\r');
    fwrite($handle, var_export($data, true));
    fwrite($handle, '\n\rPOST:\n\r');
    fwrite($handle,  var_export($_POST, true));
    fwrite($handle, '\n\rFILES:\n\r');
    fwrite($handle,  var_export($_FILES, true));    
    fclose($handle);
    $json = json_encode($data);
    header('Content-Type: application/json');
    header('Content-Length: ' . strlen($json));
    echo $json;    
}
?>
